package com.chenfan.mcn.model.base;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.chenfan.common.vo.UserVO;
import com.chenfan.mcn.config.UserVoContextHolder;
import com.chenfan.mcn.extension.mybatisplus.BaseAssembler;
import com.chenfan.common.extension.validation.groups.Modify;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

/**
 * @author: weishili
 */
@Data
public abstract class BaseCreateEntity implements BaseAssembler, Serializable {

    private static final long serialVersionUID = -6439638674674526955L;

    /**
     * 主键ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    @NotNull(message ="主键ID不能为空" , groups ={Modify.class})
    private Long id;

    /**
     * 创建人id
     */
    @TableField(value="create_by")
    private Long createBy;

    /**
     * 创建人
     */
    @TableField(value="create_name")
    private String createName;

    /**
     * 创建时间
     */
    @TableField(value="create_date")
    private Date createDate;

    /**
     * 删除状态(0-正常，1-删除)
     */
    @TableField(value="is_delete")
    private Integer isDelete;

    @Override
    public void init(){
        UserVO userVO = UserVoContextHolder.getUserVo();
        if(userVO != null){
            this.createBy = userVO.getUserId();
            this.createName = userVO.getRealName();
            this.createDate = new Date();
        } else if(this.createDate == null) {
            this.createDate = new Date();
        }
        this.isDelete = ObjectUtils.defaultIfNull(this.isDelete,0);
    }

    @Override
    public void ignore(){
        this.createBy = null;
        this.createName = null;
        this.createDate = null;
        this.isDelete = null;
    }

    /**
     * 逻辑删除填充
     * @param id
     */
    @Override
    public void logicDelete(Serializable id) {
        this.id = (Long)id;
        this.isDelete = 1;
    }
}